package com.help.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.help.dao.PushMessageDao;
import com.help.model.PushMessage;
import com.help.push.SendToMobile;
import com.help.tools.CheckDatas;
import com.help.tools.PushSeviceOnTeaPC;

public class PushToAllServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	     this.doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		 PushMessage pushMessage=new PushMessage();
		 JSONObject jsonResult=null;
		 PrintWriter out = response.getWriter(); 
		 pushMessage.setDeviceTokens(request.getParameter("devicetokens"));
		 pushMessage.setLatitude(request.getParameter("latitude"));
		 pushMessage.setLontitude(request.getParameter("lontitude"));
		 pushMessage.setAddr(request.getParameter("addr"));
		 pushMessage.setMessage(request.getParameter("message"));
		 pushMessage.setPushTime(new Date().toString());
		 pushMessage.setHasPushed(false);
		 String checkResult=CheckDatas.checkPushMessage(pushMessage);
		 //数据存在空的
		 if(!checkResult.equals("no_empty")){
			 try {
				 jsonResult=new JSONObject();
				 jsonResult.put("status", "201");
				 jsonResult.put("msg", checkResult);
				} catch (JSONException e) {
					
					e.printStackTrace();
				} 
			 out.print(jsonResult);
			 return;
		 }
		 boolean flag=PushMessageDao.getInstance().savePushMessage(pushMessage);
		 if(!flag){
			 try {
				 jsonResult=new JSONObject();
				 jsonResult.put("status", "201");
				 jsonResult.put("msg", "error_database");
				} catch (JSONException e) {
					
					e.printStackTrace();
				}  
			 out.print(jsonResult);
			 return;
		 }
		 System.out.println("flag:"+flag+" "+pushMessage.toString());
		 boolean isRightMessage=true;
		 String ticker=null;
		 String title=null;
		 String text=null;
		 try {
			JSONObject message=new JSONObject(pushMessage.getMessage());
			ticker=message.getString("ticker");
			title=message.getString("title");
			text=message.getString("text");
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			isRightMessage=false;
		}
		if(!isRightMessage){
			 try {
				 jsonResult=new JSONObject();
				 jsonResult.put("status", "201");
				 jsonResult.put("msg", "error_message");
				} catch (JSONException e) {
					
					e.printStackTrace();
				}  
			 out.print(jsonResult);
			 return;
		}
		ticker=(ticker==null?"":ticker);
		title=(title==null?"":title);
		text=(text==null?"":text);
		
		String str=null;
        try {
			//str=SendToMobile.sendAndroidBroadcast(ticker,title,text);
        	str=PushSeviceOnTeaPC.pushToAllService(ticker, title, text);
         
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
        if(str==null){
        	
			try {
				jsonResult=new JSONObject();
				jsonResult.put("status", "201");
				jsonResult.put("msg", "exception_fail");
			} catch (JSONException e) {
				
				e.printStackTrace();
			}
        }else{
        	try {
				jsonResult=new JSONObject(str);
			} catch (JSONException e) {
				e.printStackTrace();
				jsonResult=new JSONObject();
				try {
					jsonResult.put("msg", "exception_fail_2");
					jsonResult.put("status", "201");
				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
			}
        }
		 out.print(jsonResult);
	}

	

}
